package com.isunland.managesystem.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.MyPlanProgressListAdapter;
import com.isunland.managesystem.adapter.PlanManageProgressListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.MyPlanProgress;
import com.isunland.managesystem.entity.MyPlanProgressOriginal;
import com.isunland.managesystem.entity.ReplyOtherListBean;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:  @我的进程列表
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/5/2 09:49
 */
public class MyPlanProgressListFragment extends BaseListFragment {
  protected static final String EXTRA_FROM =
      MyPlanProgressListActivity.class.getSimpleName() + "EXTRA_FROM";
  protected static final String EXTRA_MYMESSAGE = "myMessage";  //@我的
  protected static final String EXTRA_MYPLAN = "myPlan";//回复我@的

  private ArrayList<MyPlanProgress> mList;
  private CurrentUser mUser;
  private String mFrom;
  private int mPage;
  private MyPlanProgressListAdapter mAdapter;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    mFrom = getArguments().getString(EXTRA_FROM);
    if (EXTRA_MYMESSAGE.equalsIgnoreCase(mFrom)) {
      setTitleCustom(R.string.myMessage);
    }
    if (EXTRA_MYPLAN.equalsIgnoreCase(mFrom)) {
      setTitleCustom(R.string.myPlan);
    }
    mList = new ArrayList<>();
    mUser = CurrentUser.newInstance(getActivity());
    mPage = 1;
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
  }

  @Override public String getUrl() {
    if (EXTRA_MYMESSAGE.equalsIgnoreCase(mFrom)) {
      return ApiConst.URL_MY_MESSAGE_PROGRESS_LIST;
    } else if (EXTRA_MYPLAN.equalsIgnoreCase(mFrom)) {
      return ApiConst.URL_MY_PLAN_PROGRESS_LIST;
    } else {
      return "";
    }
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("memberCode", mUser.getMemberCode());
    params.put("userJobNo", mUser.getJobNumber());
    params.put("pageNumber", mPage + "");
    params.put("pageSize", "20");
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    MyPlanProgressOriginal original = new Gson().fromJson(json, MyPlanProgressOriginal.class);
    ArrayList<MyPlanProgress> rows = original.getRows();
    if (Base.SUCCESS == original.getResult() && rows != null) {
      if (mPage == 1) {
        mList.clear();
      }
      mList.addAll(rows);
      if (mAdapter == null) {
        mAdapter = new MyPlanProgressListAdapter(getActivity(), mList);
        setListAdapter(mAdapter);
        return;
      }
      mAdapter.notifyDataSetChanged();
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) {
    if (PullToRefreshBase.Mode.PULL_FROM_START == refreshView.getCurrentMode()) {
      mPage = 1;
    }
    if (PullToRefreshBase.Mode.PULL_FROM_END == refreshView.getCurrentMode()) {
      mPage++;
    }
    volleyPost();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    MyPlanProgress content = mList.get(position - 1);
    Intent intent = new Intent(getActivity(), MyPlanManageProgressReplyActivity.class);
    intent.putExtra(MyPlanManageProgressReplyFragment.EXTRA_EXCID, content.getId());//进展id
    startActivity(intent);
  }

  public static MyPlanProgressListFragment newInstance(String from) {
    Bundle args = new Bundle();
    args.putString(EXTRA_FROM, from);
    MyPlanProgressListFragment fragment = new MyPlanProgressListFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
